package cn.com.guage.flink.transformation;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import com.alibaba.fastjson.JSON;

import cn.com.guage.flink.domain.Student;

/**
 * 
 * @author yangdechao map算子用法
 *
 */
public class MapTransformation2 {

	public static void main(String[] args) throws Exception {
		StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		DataStreamSource<String> textStream = env.readTextFile("data/map-data.txt");
		textStream.print();

		SingleOutputStreamOperator<String> resuilt = textStream.map(new MyMap());
		resuilt.print();
		env.execute();
	}

	public static final class MyMap implements MapFunction<String, String> {
		/**
		 * 
		 */
		private static final long serialVersionUID = -3036112274448895746L;

		public String map(String value) throws Exception {
			String[] array = value.split(",");
			return JSON.toJSONString(new Student(array[0], array[1], array[2],Integer.parseInt(array[3])));
		}

	}
}
